package com.qhzz.permissionsmanagerdemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.qhzz.permissionlib.AndPermission;
import com.qhzz.permissionlib.OnGrantListener;
import com.qhzz.permissionlib.Permission;
import com.qhzz.permissionlib.PermissionUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_calendarApply;
    private Button btn_cameraApply;
    private Button btn_contactsApply;
    private Button btn_locationApply;
    private Button btn_microphoneApply;
    private Button btn_phoneApply;
    private Button btn_sensorsApply;
    private Button btn_smsApply;
    private Button btn_storageApply;
    private Button btn_moreApply;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_calendarApply = (Button) findViewById(R.id.btn_calendarApply);
        btn_cameraApply = (Button) findViewById(R.id.btn_cameraApply);
        btn_contactsApply = (Button) findViewById(R.id.btn_contactsApply);
        btn_locationApply = (Button) findViewById(R.id.btn_locationApply);
        btn_microphoneApply = (Button) findViewById(R.id.btn_microphoneApply);
        btn_phoneApply = (Button) findViewById(R.id.btn_phoneApply);
        btn_sensorsApply = (Button) findViewById(R.id.btn_sensorsApply);
        btn_smsApply = (Button) findViewById(R.id.btn_smsApply);
        btn_storageApply = (Button) findViewById(R.id.btn_storageApply);
        btn_moreApply = (Button) findViewById(R.id.btn_moreApply);

        btn_calendarApply.setOnClickListener(this);
        btn_cameraApply.setOnClickListener(this);
        btn_contactsApply.setOnClickListener(this);
        btn_locationApply.setOnClickListener(this);
        btn_microphoneApply.setOnClickListener(this);
        btn_phoneApply.setOnClickListener(this);
        btn_sensorsApply.setOnClickListener(this);
        btn_smsApply.setOnClickListener(this);
        btn_storageApply.setOnClickListener(this);
        btn_moreApply.setOnClickListener(this);
    }

    /**
     * 日历权限申请
     */
    private void calendarApply() {
        PermissionUtils.calendarApply(this, new OnGrantListener() {
            @Override
            public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                Toast.makeText(MainActivity.this, "日历", Toast.LENGTH_SHORT).show();
            }

        });
    }

    /**
     * 相机权限申请
     */
    private void cameraApply() {
        PermissionUtils.cameraApply(this, new OnGrantListener() {
            @Override
            public void onGrant(int requestCode, @NonNull String... grantPermissions) {

                Toast.makeText(MainActivity.this, "相机", Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 通讯录权限申请
     */
    private void contactsApply() {
        PermissionUtils.contactsApply(this, new OnGrantListener() {
            @Override
            public void onGrant(int requestCode, @NonNull String... grantPermissions) {

                Toast.makeText(MainActivity.this, "通许录", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 位置权限申请
     */
    private void locationApply() {

        AndPermission.with(this)
                .permission(Permission.LOCATION)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "位置", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 音频录制权限申请
     */
    private void microphoneApply() {
        AndPermission.with(this)
                .permission(Permission.MICROPHONE)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "音频录制", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 电话权限申请
     */
    private void phoneApply() {
        AndPermission.with(this)
                .permission(Permission.PHONE)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "电话", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 传感器权限申请
     */
    private void sensorsApply() {
        AndPermission.with(this)
                .permission(Permission.SENSORS)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "传感器", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 短信权限申请
     */
    private void smsApply() {
        AndPermission.with(this)
                .permission(Permission.SMS)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "短信权限", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 外部存储权限申请
     */
    private void storageApply() {
        AndPermission.with(this)
                .permission(Permission.STORAGE)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "外部储存", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    /**
     * 多个权限同时申请
     */
    private void moreApply() {
        AndPermission.with(this)
                .permission(Permission.CAMERA, Permission.STORAGE)
                .setOnGrantListener(new OnGrantListener() {
                    @Override
                    public void onGrant(int requestCode, @NonNull String... grantPermissions) {
                        Toast.makeText(MainActivity.this, "多个权限", Toast.LENGTH_SHORT).show();
                    }
                })
                .start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_calendarApply:
                calendarApply();
                break;
            case R.id.btn_cameraApply:
                cameraApply();
                break;
            case R.id.btn_contactsApply:
                contactsApply();
                break;
            case R.id.btn_locationApply:
                locationApply();
                break;
            case R.id.btn_microphoneApply:
                microphoneApply();
                break;
            case R.id.btn_phoneApply:
                phoneApply();
                break;
            case R.id.btn_sensorsApply:
                sensorsApply();
                break;
            case R.id.btn_smsApply:
                smsApply();
                break;
            case R.id.btn_storageApply:
                storageApply();
                break;
            case R.id.btn_moreApply:
                moreApply();
                break;
        }
    }
}
